package lobby.component.combobox
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import lobby.exchangeddata.HashTable;
	
	/**
	 * ...
	 * @author Nguyen Hoang Giang (Tom)
	 */
	public class PannelCombobox extends MovieClip
	{
		public var itemSelectedMc:MovieClip;
		public var boardItem:MovieClip;
		private var selectedDown:Boolean = true;
		public var data:Array = new Array();
		public var childrens:Array = new Array();
		private var selectedName:String;
		
		public function PannelCombobox()
		{
			itemSelectedMc.addEventListener(MouseEvent.CLICK, hldItemSelectedMc);
			itemSelectedMc.buttonMode = true;
			boardItem.visible = false;
			boardItem.addEventListener(MouseEvent.ROLL_OUT, hldBoardItem);
			itemSelectedMc.txtTf.mouseEnabled = false;
		}
		
		private function hldBoardItem(e:MouseEvent):void
		{
			boardItem.visible = false;
			selectedDown = true;
			itemSelectedMc.btnUpDown.gotoAndStop("up");
		}
		
		private function hldItemSelectedMc(e:MouseEvent):void
		{
			if (selectedDown)
			{
				selectedDown = false;
				itemSelectedMc.btnUpDown.gotoAndStop("down");
				boardItem.visible = true;
			}
			else
			{
				selectedDown = true;
				itemSelectedMc.btnUpDown.gotoAndStop("up");
				boardItem.visible = false;
			}
		}
		
		public function initCombobox(data:Array, name:String, value:String):void
		{
			var temp:flash.display.Sprite;
			for (var i:int = 0; i < data.length; i++)
			{
				var item:ItemCombobox = new ItemCombobox();
				item.name = data[i][name];
				item.txtTf.text = data[i][value];
				item.x = boardItem.x;
				item.y = (item.height - 1) * i;
				item.icon.gotoAndStop(item.name);
				item.addEventListener(MouseEvent.CLICK, hldItemCombobox);
				boardItem.addChild(item);
				childrens.push(item);
			}
			boardItem.tabChildren = false;
			var evt:EventCombobox = new EventCombobox(EventCombobox.COMPLETED_INIT_ITEM, true);
			dispatchEvent(evt);
		}
		
		private function hldItemCombobox(e:MouseEvent):void
		{
			if (this.selectedName != e.currentTarget.name)
			{
				itemSelectedMc.txtTf.text = e.currentTarget.txtTf.text;
				var evt:EventCombobox = new EventCombobox(EventCombobox.CHANGED, true);
				evt.currentName = e.currentTarget.name;
				dispatchEvent(evt);
				this.selectedName = e.currentTarget.name;
				this.boardItem.visible = false;
				itemSelectedMc.icon.gotoAndStop(e.currentTarget.name);
			}
		}
		
		public function setDefaultItem(name:String):void
		{
			var item:ItemCombobox;
			for (var i:int = 0; i < childrens.length; i++)
			{
				item = childrens[i];
				if (item.name == name)
				{
					this.selectedName = name;
					itemSelectedMc.txtTf.text = item.txtTf.text;
					itemSelectedMc.icon.gotoAndStop(item.name);
					break;
				}
			}
		}
	}

}